package com.ssh.cp.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.springframework.web.multipart.MultipartFile;

public class FileUtil {
	public static final String DEFAULT_FILE_SAVEPATH="d:"+File.separator+"temp"+File.separator;
	
	FileUtil(){
		File f=new File(DEFAULT_FILE_SAVEPATH);
		if(!f.exists())
			f.mkdirs();
	}
	
	public static String uploadFile(File uploadfile,String path,String defaultFileName) {
		FileOutputStream fos = null;
        FileInputStream fis = null;
		String filePath=path+defaultFileName;
		
		File parent=new File(filePath).getParentFile();
		if(!parent.exists()) {
			parent.mkdirs();
		}
		
		//String fileName=uploadfile.getName();
		try {
            // 建立文件输出流            
            fos = new FileOutputStream(filePath);
            // 建立文件上传流
            fis = new FileInputStream(uploadfile);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            System.out.println("文件上传失败");
            e.printStackTrace();
        } finally {
            close(fos, fis);
        }
        return "success";
	
	}
	
	
	   public static void zipMultiFile(String filepath ,String zippath) {
	        try {
	            File file = new File(filepath);// 要被压缩的文件夹
	            File zipFile = new File(zippath);
	            ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
	            if(file.isDirectory()){
	                File[] files = file.listFiles();
	                for(File fileSec:files){
	                    if(fileSec.isDirectory()){
	                        recursionZip(zipOut, fileSec, file.getName() + File.separator);
	                    }else{
	                        recursionZip(zipOut, fileSec, "");
	                    }
	                }
	            }
	            zipOut.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
	     
	    private static void recursionZip(ZipOutputStream zipOut, File file, String baseDir) throws Exception{
	        if(file.isDirectory()){
	            File[] files = file.listFiles();
	            for(File fileSec:files){
	                recursionZip(zipOut, fileSec, baseDir + file.getName() + File.separator);
	            }
	        }else{
	            byte[] buf = new byte[1024];
	            InputStream input = new FileInputStream(file);
	            zipOut.putNextEntry(new ZipEntry(baseDir + file.getName()));
	            int len;
	            while((len = input.read(buf)) != -1){
	                zipOut.write(buf, 0, len);
	            }
	            input.close();
	        }
	    }
	
	
	
	
	public static boolean uploadFile2(MultipartFile file,String filePath,String fileName) {
		File targetFile = new File(filePath+fileName);
		 if (!targetFile.getParentFile().exists()) {
			 targetFile.getParentFile().mkdirs();
	        }
		 try {
	            file.transferTo(targetFile);
	        } catch (IOException e) {
	            //return "后台也不知道为什么, 反正就是上传失败了";
	        	return false;
	        }
	        //return "上传成功";
		 return true;
	    }
	
	private static void close(FileOutputStream fos, FileInputStream fis) {
        if (fis != null) {
            try {
            	
                fis.close();
                System.out.println("FileInputStream关闭成功");
            } catch (IOException e) {
                System.out.println("FileInputStream关闭失败");
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
                System.out.println("FileOutputStream关闭成功");
            } catch (IOException e) {
                System.out.println("FileOutputStream关闭失败");
                e.printStackTrace();
            }
        }
    }
	}
	
	

